<?php
function conecta_mysql() {
	$conexao = mysql_connect("localhost:3306","root", "9651myla");
	mysql_select_db("ccv");

	if (!$conexao) {
		die('Não foi possível conectar: ' . mysql_error());
	}
}

function escreve_cabecalho ($titulo){
	echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n".
"<html>\n".
"<head>\n".
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n".
"<link href=\"css/global.css\" rel=\"stylesheet\" type=\"text/css\" />\n".
"<script type=\"text/javascript\" src=\"./js/funcoes.js\"></script>\n".
"<script type=\"text/javascript\" src=\"./js/jquery-1.9.1.min.js\"></script>\n".
"<script type=\"text/javascript\" src=\"./js/jquery.maskMoney.js\"></script>\n".
"<script type=\"text/javascript\" src=\"./js/jquery.cpf-validate.1.0.min.js\"></script>\n".
"<script type=\"text/javascript\" src=\"./js/jquery.maskedinput.min.js\"></script>\n".
"<script type=\"text/javascript\" src=\"./js/jquery.validate.js\"></script>\n".
"<title>".$titulo."</title>\n".
"<body>\n".
"<table width=\"100%\" align=\"center\">\n".
"<tr>\n".
"    <td height=\"77\" align=\"left\" valign=\"top\"><a href=\"/prestacao_de_contas/pagina_principal.php\"><img src=\"img/Sudeco-Logo-PNG.png\" width=\"150\" height=\"48\" alt=\"Voltar para página inicial\" title=\"Voltar para p�gina inicial\"border=\"0\"></a></td>\n".
"<td align=\"right\" valign=\"top\"><a href=\"/prestacao_de_contas/logout.php\" target=\"_top\">Encerrar sessão</a><br>\n".
"</td>\n".
"  </tr>\n".
"</table>\n".
"</tr>\n";
}

function verifica_sessao(){
	session_start();
	if((!isset ($_SESSION['login']) == true))
	{
		unset($_SESSION['login']);
		header('location:index.php');
	}
}

function login($login, $senha){
	include 'conecta_mysql.php';
        $senhaM = md5($senha);

	$result = mysql_query("SELECT * FROM usuario WHERE login = '$login' AND senha = '$senhaM'",$conexao);
	$linhas = mysql_num_rows($result);
	if($linhas > 0 )
	{
		
		$cadastro_usuario = mysql_result($result, 0,"cadastro_usuario");
		
		session_start();
		$_SESSION['login'] = $login;
		$_SESSION['cadastro_usuario'] = $cadastro_usuario;
		
		header('location:pagina_principal.php');
	}
	else{
		unset ($_SESSION['login']);
		echo "<Script language=\"javascript\">window.alert('Usuario ou senha incorretos.!');</script>";
		header('location:index.php');

	}
}

function logout(){
	session_start();
	$_SESSION = array();
	session_destroy();
	header("Location: index.php");

}

function select_uf($area, $valor){
	if ($area == 'co'){
		echo
	"<select name=\"uf\" id=\"uf\"><option value=\"\">SELECIONE O ESTADO</option>";
		echo "<option value=\"df\" ";
		if ($valor == 'df') echo ('selected = "selected"');
		echo ">DF - Distrito Federal</option>\n".
	"<option value=\"go\"";
		if ($valor == 'go') echo ('selected = "selected"');
		echo ">GO - Goiás</option>\n".
	"<option value=\"ms\"";
		if ($valor == 'ms') echo ('selected = "selected"');
		echo ">MS - Mato Grosso do Sul</option>\n".
	"<option value=\"mt\"";
		if ($valor == 'mt') echo ('selected = "selected"');
		echo ">MT - Mato Grosso</option>\n".
		"<option value=\"mg\"";
		if ($valor == 'mg') echo ('selected = "selected"');
		echo ">MG - Minas Gerais</option>\n".
		"</select>";
	}
	else if ($area == 'br'){
		echo
		"<select name=\"uf\" id=\"uf\">\n".
		"<option value=\"\">SELECIONE O ESTADO</option>\n".
		"<option value=\"ac\">AC - Acre</option>\n".
		"<option value=\"al\">AL - Alagoas</option>\n".
		"<option value=\"am\">AM - Amazonas</option>\n".
		"<option value=\"ap\">AP - Amapá</option>\n".
		"<option value=\"ba\">BA - Bahia</option>\n".
		"<option value=\"ce\">CE - Ceará</option>\n".
		"<option value=\"df\">DF - Distrito Federal</option>\n".
		"<option value=\"es\">ES - Espírito Santo</option>\n".
		"<option value=\"go\">GO - Goiás</option>\n".
		"<option value=\"ma\">MA - Maranhão</option>\n".
		"<option value=\"mt\">MT - Mato Grosso</option>\n".
		"<option value=\"ms\">MS - Mato Grosso do Sul</option>\n".
		"<option value=\"mg\">MG - Minas Gerais</option>\n".
		"<option value=\"pa\">PA - Pará</option>\n".
		"<option value=\"pb\">PB - Paraíba</option>\n".
		"<option value=\"pr\">PR - Paraná</option>\n".
		"<option value=\"pe\">PE - Pernambuco</option>\n".
		"<option value=\"pi\">PI - Piauí</option>\n".
		"<option value=\"rj\">RJ - Rio de Janeiro</option>\n".
		"<option value=\"rn\">RN - Rio Grande do Norte</option>\n".
		"<option value=\"ro\">RO - Rondônia</option>\n".
		"<option value=\"rs\">RS - Rio Grande do Sul</option>\n".
		"<option value=\"rr\">RR - Roraima</option>\n".
		"<option value=\"sc\">SC - Santa Catarina</option>\n".
		"<option value=\"se\">SE - Sergipe</option>\n".
		"<option value=\"sp\">SP - São Paulo</option>\n".
		"<option value=\"to\">TO - Tocantins</option>\n".
		"</select>";
	}
}

function data_banco ($data){
	if ($data == ''|| $data == null){
		return "null";
	}
	elseif (preg_match('#(\d{1,2})\D(\d{1,2})\D(\d{4})#',$data,$match)){
		$time = mktime(0,0,0,$match[2],$match[1],$match[3]);
		return "'".date('Y-m-d',$time)."'";
	}
}

function data_banco_relatorio ($data){
	if ($data == ''|| $data == null){
		return "null";
	}
	elseif (preg_match('#(\d{1,2})\D(\d{1,2})\D(\d{4})#',$data,$match)){
		$time = mktime(0,0,0,$match[2],$match[1],$match[3]);
		return date('Y-m-d',$time);
	}
}

function data_sistema($data){
	return date("d/m/Y", strtotime($data));
}

function data_sistema_hora($data){
	return date("d/m/Y H:i:s", strtotime($data));
}

function formata_ponto($variavel){
	if ($variavel == '' || $variavel == null){
		$variavel = '0,00';
	}
	return str_replace(",", ".", $variavel);
}
function formata_numero ($variavel){
	return number_format($variavel, 2, ',', '.');
}

function escreve_parcela($processo_numero){
	include 'conecta_mysql.php';
	$sql_parcela = "SELECT parcela_numero, parcela_valor FROM parcela_pagamento
						where processo = '$processo_numero' order by parcela_numero";
	$result_parcela = mysql_query($sql_parcela, $conexao);

	while (list ($parcela_numero, $parcela_valor) = mysql_fetch_row ($result_parcela)) {
		$parcela_formatada = formata_numero($parcela_valor);
		echo "<script type=\"text/javascript\">";
		echo "form_altera.parcela".$parcela_numero.".value = \"".$parcela_formatada."\";";
		//echo "moeda(parcela".$parcela_numero.")";
	 echo "</script>";
		echo "<input type=\"hidden\" name=\"p$parcela_numero\" id=\"p$parcela_numero\"value = \"s\">";
	}}

	function retira_mascara_numero ($variavel){
		$numero = str_replace(".", "",$variavel);
		return str_replace(",", ".",$numero);
	}
	?>